<template>
    <!--选课中心-->
    <div class="contain">
        <!--分类搜索栏-->
        <div class="m-itemize" v-if="!firstItem">
            <div class="itemize-contain">
                <span class="itemize-name">项目</span>
                <ul class="itemize-list">
                    <li
                        class="itemize-total"
                        :class="{ active: itemSel === '' }"
                        @click="handleChangeItemSel('')"
                    >
                        全部
                    </li>
                    <li
                        class="item-opt"
                        v-for="item in itemList"
                        :key="item.id"
                        :class="{ active: itemSel === item.first_name }"
                        @click="handleChangeItemSel(item.first_name)"
                    >
                        {{ item.first_name }}
                    </li>
                </ul>
            </div>
            <div class="itemize-contain">
                <span class="itemize-name">科目</span>
                <ul class="itemize-list">
                    <li
                        class="itemize-total"
                        :class="{ active: subjectSel === '' }"
                        @click="handleChangeSubjectSel('')"
                    >
                        全部
                    </li>
                    <li
                        class="item-opt"
                        v-for="(item, index) in subjectList"
                        :key="index"
                        :class="{ active: subjectSel === item.sec_name }"
                        @click="handleChangeSubjectSel(item.sec_name)"
                    >
                        {{ item.sec_name }}
                    </li>
                </ul>
            </div>
            <div class="itemize-contain">
                <span class="itemize-name">价格</span>
                <ul class="itemize-list">
                    <li
                        class="itemize-total"
                        :class="{ active: priceSel === 0 }"
                        @click="handleChangePriceSel(0)"
                    >
                        综合
                    </li>
                    <li
                        class="item-opt"
                        :class="{ active: priceSel === 1 }"
                        @click="handleChangePriceSel(1)"
                    >
                        从高到低
                    </li>
                    <li
                        class="item-opt"
                        :class="{ active: priceSel === 2 }"
                        @click="handleChangePriceSel(2)"
                    >
                        从低到高
                    </li>
                </ul>
            </div>
            <div class="itemize-contain">
                <span class="itemize-name">热度</span>
                <ul class="itemize-list">
                    <li
                        class="itemize-total"
                        :class="{ active: hotSel === 0 }"
                        @click="handleChangehotSel(0)"
                    >
                        综合
                    </li>
                    <li
                        class="item-opt"
                        :class="{ active: hotSel === 1 }"
                        @click="handleChangehotSel(1)"
                    >
                        最新
                    </li>
                    <li
                        class="item-opt"
                        :class="{ active: hotSel === 2 }"
                        @click="handleChangehotSel(2)"
                    >
                        最热
                    </li>
                </ul>
            </div>
        </div>
        <!--搜索结果-->
        <div class="m-result-contain">
            <ul>
                <li
                    class="product-intro"
                    v-for="(product, index) in productList"
                    :key="index"
                >
                    <div class="img-warp">
                        <img @click="handleClickProductDetail(product.id)" :src="product.fengmian_pic_url" alt=""/>
                    </div>
                    <div class="product-name">{{ product.name }}</div>
                    <div class="product-desc">
                        {{ product.good_desc }}
                    </div>
                    <div
                        class="product-detail"
                        @click="handleClickProductDetail(product.id)"
                    >
                        查看详情
                    </div>
                    <div class="product-price">
                        <span class="price-unit">￥</span>
                        <span class="price">{{ product.one_price | fixNumber }}</span>
                        <span class="price-origin">￥{{ product.deadline_price | fixNumber }}</span>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</template>
<script>
export default {
    name: 'courseCenter',
    components: {
        /**/
    },
    data() {
        return {
            account: '',
            itemList: [],
            subjectList: [],
            itemSel: '',
            subjectSel: '',
            priceSel: 0,
            hotSel: 0,
            productList: []
        };
    },
    props: {
        firstItem: null,
        secondItem: null
    },
    watch: {
        firstItem(val) {
            if (val) {
                this.itemSel = val
                this.ajaxGetList();
            }
        },
        secondItem() {

        }
    },
    mounted() {
        if (!this.$util.authLogin()) {
            location.replace('/login');
        } else {
            if (this.firstItem && !this.secondItem) {

            } else {
                this.initPage();
            }
        }
    },
    methods: {
        initPage() {
            this.ajaxGetZsList();
            this.ajaxGetsubjectList();
            this.ajaxGetList();
        },
        // 获取一级科目标签
        ajaxGetZsList() {
            let option = {
                pathName: 'doGetFLabs'
            };
            this.$api.ajaxData(option).then(res => {
                this.itemList = res.obj;
            });
        },
        // 获取二级科目标签
        ajaxGetsubjectList() {
            let option = {
                pathName: 'doGetSLabs'
            };
            this.$api.ajaxData(option).then(res => {
                this.subjectList = res.obj;
            });
        },
        // 查询产品列表
        ajaxGetList() {
            this.$api.ajaxData({
                pathName: 'doGetGoodsXk',
                authorization: true,
                params: {
                    first_lab_name: this.itemSel || '',
                    secend_lab_name: this.subjectSel || '',
                    jgpx: this.priceSel || 0,
                    is_hot: this.hotSel || 0
                },
                errorFunc: false
            })
                .then(res => {
                    this.productList = res.obj;
                })
                .catch(() => {
                    this.productList = [];
                });
        },
        handleChangeItemSel(val) {
            this.itemSel = val;
            this.ajaxGetList();
        },
        handleChangeSubjectSel(val) {
            this.subjectSel = val;
            this.ajaxGetList();
        },
        handleChangePriceSel(val) {
            this.priceSel = val;
            this.ajaxGetList();
        },
        handleChangehotSel(val) {
            this.hotSel = val;
            this.ajaxGetList();
        },
        // 查看详情
        handleClickProductDetail(id) {
            let routeData = this.$router.resolve({name: 'landingPage', query: {id: id}});
            window.open(routeData.href, '_blank');
        }
    }
};
</script>
<style scoped lang="scss">
.contain {
    padding-top: 20px;

    .m-itemize {
        position: relative;
        padding: 0 0 10px;
        background: #fff;
        box-shadow: 0 2px 15px rgba(0, 0, 0, 0.05);
        z-index: 1;

        .itemize-contain {
            max-width: 1366px;
            display: flex;
            min-height: 70px;
            padding: 10px 0;
            margin: 0 auto;
            align-items: center;
            box-sizing: border-box;

            &:not(:last-child) {
                border-bottom: 1px solid #f2f2f2;
            }

            .itemize-name {
                flex: 0 0 80px;
                font-size: 14px;
                font-weight: bold;
                text-align: center;
            }

            .itemize-list {
                position: relative;
                display: flex;
                padding: 0 0 0 95px;
                flex-wrap: wrap;
                font-size: 13px;
                color: #333;

                li {
                    padding: 0 10px;
                    height: 30px;
                    line-height: 30px;
                    text-align: center;
                    border-radius: 5px;
                    box-sizing: border-box;
                    cursor: pointer;

                    &:first-child {
                        position: absolute;
                        left: 0;
                        top: 50%;
                        width: 80px;
                        margin: -15px 0 0 0;
                    }

                    &.active {
                        color: #0079fe;
                        background: #b3d7ff;
                    }
                }
            }
        }
    }

    .m-result-contain {
        padding: 20px 0 0;

        ul {
            display: flex;
            flex-wrap: wrap;
            padding: 0 20px;

            .product-intro {
                position: relative;
                width: 23%;
                margin: 0 1% 20px;
                padding: 12px;
                border-radius: 10px;
                background: #fff;
                border: 1px solid rgba(242, 242, 242, 1);

                .img-warp {
                    position: relative;
                    width: 100%;
                    padding-top: 55.5%;
                    background: rgba(242, 242, 242, 1);

                    img {
                        position: absolute;
                        top: 0;
                        left: 0;
                        display: block;
                        width: 100%;
                        height: 100%;
                    }
                }

                .product-name {
                    margin: 15px 0 0;
                    height: 24px;
                    font-size: 16px;
                    line-height: 1.5;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }

                .product-desc {
                    height: 42px;
                    margin-bottom: 10px;
                    @include ellipsis-line;
                }

                .product-detail {
                    float: right;
                    width: 87px;
                    height: 30px;
                    line-height: 30px;
                    border-radius: 15px;
                    font-size: 13px;
                    cursor: pointer;
                    color: #fff;
                    background: #ff8080;
                    text-align: center;
                }

                .product-price {
                    display: flex;
                    flex-wrap: wrap;
                    line-height: 22px;
                    font-weight: 700;
                    font-style: normal;
                    font-size: 18px;
                    color: #ff9900;

                    .price-origin {
                        padding-left: 10px;
                        font-weight: 400;
                        font-style: normal;
                        font-size: 13px;
                        color: #d7d7d7;
                        text-decoration: line-through;
                    }
                }
            }
        }
    }
}
</style>
